﻿#include "ssColor.h"
#include "ssAssert.h"

namespace StarSeeker
{

ssColor4::ssColor4(void)
{
	x = y = z = w = 0.f;
}
ssColor4::ssColor4(ssfloat fr, ssfloat fg, ssfloat fb, ssfloat fa)
{
	x = fr;
	y = fg;
	z = fb;
	w = fa;
}
ssColor4::ssColor4(const ssColor4 &sf4)
{
	x = sf4.x;
	y = sf4.y;
	z = sf4.z;
	w = sf4.w;
}
void ssColor4::operator=(const ssColor4 &sf4)
{
	x = sf4.x;
	y = sf4.y;
	z = sf4.z;
	w = sf4.w;
}

ssColor4::operator ssfloat* ()
{
	return m;
}
ssColor4::operator const ssfloat* () const
{
	return m;
}

void ssColor4::Set(ssfloat fr, ssfloat fg, ssfloat fb, ssfloat fa)
{
	x = fr;
	y = fg;
	z = fb;
	w = fa;
}
void ssColor4::Set(const ssfloat *fVal)
{
	x = fVal[0];
	y = fVal[1];
	z = fVal[2];
	w = fVal[3];
}
void ssColor4::Zero()
{
	x = y = z = w = 0.f;
}
//点乘
ssfloat ssColor4::Dot(const ssColor4& val) const
{
	return x*val.x + y*val.y + z*val.z + w*val.w;
}
//叉乘
ssColor4 ssColor4::Cross(const ssColor4& val) const
{
	ssColor4 q;
	q.w = w*val.w - x*val.x - y*val.y - z*val.z;
	q.x = w*val.x + x*val.w + z*val.y - y*val.z;
	q.y = w*val.y + y*val.w + x*val.z - z*val.x;
	q.z = w*val.z + z*val.w + y*val.x - x*val.y;
	return q;
}
//近似0向量
bool ssColor4::IsZero(ssfloat eps)
{
	return (Math3D::Abs(x) < eps && Math3D::Abs(y) < eps && Math3D::Abs(z) < eps && Math3D::Abs(w) < eps);
}
//两个向量近似相等
bool ssColor4::IsApproximate(const ssColor4& val, ssfloat eps)
{
	return (Math3D::Abs(x - val.x) < eps && Math3D::Abs(y - val.y) < eps && Math3D::Abs(z - val.z) < eps && Math3D::Abs(w - val.w) < eps);
}
//单位化
void ssColor4::Black()
{
	x = y = z = 0.f;
	w = 1.f;
}
//求模
ssfloat ssColor4::Length() const
{
	return Math3D::Sqrt(x*x + y*y + z*z + w*w);
}
//单位化
void ssColor4::Normalize()
{
	ssfloat len = Length();
	SS_ASSERT(len != 0.0f);
	if (len != 0.0f)
	{
		x /= len;
		y /= len;
		z /= len;
		w /= len;
	}
}

ssColor4 ssColor4::operator*(ssfloat val) const
{
	ssColor4 q(x*val, y*val, z*val, w*val);
	return q;
}

ssColor4 ssColor4::operator/(ssfloat val) const
{
	ssColor4 q(x/val, y/val, z/val, w/val);
	return q;
}

void ssColor4::operator*=(ssfloat val)
{
	x *= val;
	y *= val;
	z *= val;
	w *= val;
}

void ssColor4::operator/=(ssfloat val)
{
	x /= val;
	y /= val;
	z /= val;
	w /= val;
}

}
